<?php
/**
 * Description of Yxb 微信服务号接口
 * @author zhang
 */
namespace Home\Controller;
use Think\Controller;
use Common\Extend\Weixin\TPWechat;
use Common\Extend\wxTsMsg;
use Think\Log;

class WxController extends Controller {

    private $appid;
    private $appsecret;

    public function __construct()
    {
        header("Content-Type: text/html; charset=UTF-8");
        $this->appid = 'wxd4941f0c17f04be4'; //开放平台appid
        $this->appsecret = 'a2a575f41a1b26a0aa9d769259eb3275';//开放平台appsecret
    }

    //https://sj.258.com/?auth_code=queryauthcode@@@pV6TuBSAjVYFDMFci7ir9Aas1oyUdVUFUhQrmlxALxJbrPMKxZHpu9McUg6jSY-Zt1g-J4WSFioAZoSfe82zag&expires_in=3600
    /**
     * 意向客户微信消息推送接口
     * @param $company_name
     * @param $contact_name
     * @param $contact_mobile
     * @param $content
     * @param $next_content
     * @param $next_time
     * @param $openid
     * @return array
     */
    public function wxNotice() {
        $token= md5('_'.md5(substr(time(), 0,4)).'_PlIM-lJ54-940R-J2K8-JudM');
        $wx_tpl['token'] = I('post.token');  //token
        $wx_tpl['company_name'] = I('post.company_name');  //意向客户名称
        $wx_tpl['contact_name'] = I('post.contact_name');  //意向客户联系人
        $wx_tpl['contact_mobile'] = I('post.contact_mobile'); //意向客户手机
        $wx_tpl['content'] = I('post.content');   //意向客户跟进内容
        $wx_tpl['next_content'] = I('post.next_content');  //意向客户下一次跟进内容
        if(!$wx_tpl['next_content']){
            $wx_tpl['next_content']='待跟进';
        }
        $wx_tpl['next_time'] = I('post.next_time');  //意向客户下一次跟进时间
        if(!$wx_tpl['next_time']){
            $wx_tpl['next_time']='待跟进';
        }else{
            $wx_tpl['next_time']=date('Y-m-d',$wx_tpl['next_time']);
        }
        $wx_tpl['openid'] = I('post.openid');   //服务号openid
        $openid = explode(',',$wx_tpl['openid']);
        _log($wx_tpl,'wxNotice');
        if($token !== $wx_tpl['token']){
            $aa['s']=-2;
            $aa['state']=false;
            $aa['msg']='非法请求';
            echo json_encode($aa);exit;
        }elseif(!$wx_tpl['company_name']&& !$wx_tpl['openid']){
            $aa['s']=-1;
            $aa['state']=false;
            $aa['msg']='请求失败，没有数据';
            echo json_encode($aa);exit;
        }else{
            $wxTsMsg = new wxTsMsg();
            foreach($openid as $v){
                $component_access_token = $wxTsMsg->get_component_access_token();
                //获取预授权码使用
                /* $pre_auth_code = $wxTsMsg->get_pre_auth_code($component_access_token);
                 $auth_info = $wxTsMsg->get_authorization($pre_auth_code,$component_access_token);*/
                $authorizer_access_token=$wxTsMsg->get_authorizer_token('wx2ab9b24639084881','refreshtoken@@@_z7wp-4PEOUp2Ijs2DkvdKuYaBhVrV9Xb0gyoOhj8VM',$component_access_token);
                $datas=[
                    'touser'=>$v,
                    'template_id'=>'jupIHCatAg4B3IdgGZke-6OrSi8aFeLfwpvUJ-i_jWM',
                    'url'=>'',
                    'miniprogram'=>[
                        'appid'=>'wx29b3dfa4747d03df',
                        'pagepath'=>'pages/index/login/login',
                    ],
                    'data'=>array(
                        'first'=>array(
                            "value"=>'您有一个意向客户',
                            "color"=>""
                        ),
                        "keyword1"=>array(
                            "value"=>$wx_tpl['company_name'],
                            "color"=>""
                        ),
                        "keyword2"=>array(
                            "value"=>$wx_tpl['contact_name'],
                            "color"=>""
                        ),
                        "keyword3"=>array(
                            "value"=>$wx_tpl['content'],
                            "color"=>""
                        ),
                        "keyword4"=>array(
                            "value"=>$wx_tpl['next_time'],
                            "color"=>""
                        ),
                        "keyword5"=>array(
                            "value"=>$wx_tpl['next_content'],
                            "color"=>""
                        ),
                        "remark"=>array(
                            "value"=>"请及时查看跟进客户",
                            "color"=>""
                        )
                    ),
                ];
                $url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token='.$authorizer_access_token;
                $res = https_request($url,$datas,'json');
                _log($res,'wxNotice');
            }
        }
        $aa['s']=1;
        $aa['state']=true;
        $aa['msg']='数据获取成功';
        echo json_encode($aa);exit;
    }

    //发送模板消息
    public function send_msg($touser,$template_id,$url,$appid,$pagepath,$authorizer_access_token){
        $datas=[
            'touser'=>$touser,
            'template_id'=>$template_id,
            'url'=>$url,
            'miniprogram'=>[
                'appid'=>$appid,
                'pagepath'=>$pagepath,
            ],
            'data'=>[
                'first'=>'请及时跟踪客户',
                'keyword1'=>'营销宝',
                'keyword2'=>'营销宝',
                'keyword3'=>'电话联系人客户对方案是否满意',
                'keyword4'=>'2016年12月10日',
                'keyword5'=>'询问客户是否能根据方案签合同',
                'remark'=>'请及时跟踪'
            ]
        ];
        $url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token='.$authorizer_access_token;
        $res = https_request($url,$datas,'json');
        return $res;
    }

    /**
     * 消息推送测试
     * author rhett_ni
     */
    public function wxtest() {
        $openid = ['o-MV4t3lFQrRXjt-2wce97oKe7z4'];
        $wx_tpl = [
            'company_name' => '测试',
            'contact_name' => '测试',
            'content' => '测试',
            'next_time' => '测试',
            'next_content' => '测试'
        ];
        $wxTsMsg = new wxTsMsg();
        foreach($openid as $v){
            $component_access_token = $wxTsMsg->get_component_access_token();
            //获取预授权码使用
            /* $pre_auth_code = $wxTsMsg->get_pre_auth_code($component_access_token);
             $auth_info = $wxTsMsg->get_authorization($pre_auth_code,$component_access_token);*/
            $authorizer_access_token=$wxTsMsg->get_authorizer_token('wx2ab9b24639084881','refreshtoken@@@_z7wp-4PEOUp2Ijs2DkvdKuYaBhVrV9Xb0gyoOhj8VM',$component_access_token);
            $datas=[
                'touser'=>$v,
                'template_id'=>'jupIHCatAg4B3IdgGZke-6OrSi8aFeLfwpvUJ-i_jWM',
                'url'=>'',
                'miniprogram'=>[
                    'appid'=>'wx29b3dfa4747d03df',
                    'pagepath'=>'pages/index/login/login',
                ],
                'data'=>array(
                    'first'=>array(
                        "value"=>'您有一个意向客户',
                        "color"=>""
                    ),
                    "keyword1"=>array(
                        "value"=>$wx_tpl['company_name'],
                        "color"=>""
                    ),
                    "keyword2"=>array(
                        "value"=>$wx_tpl['contact_name'],
                        "color"=>""
                    ),
                    "keyword3"=>array(
                        "value"=>$wx_tpl['content'],
                        "color"=>""
                    ),
                    "keyword4"=>array(
                        "value"=>$wx_tpl['next_time'],
                        "color"=>""
                    ),
                    "keyword5"=>array(
                        "value"=>$wx_tpl['next_content'],
                        "color"=>""
                    ),
                    "remark"=>array(
                        "value"=>"请及时查看跟进客户",
                        "color"=>""
                    )
                ),
            ];
            $url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token='.$authorizer_access_token;
            $res = https_request($url,$datas,'json');
            _debug($res);
        }
        $aa['s']=1;
        $aa['state']=true;
        $aa['msg']='数据获取成功';
        echo json_encode($aa);exit;
    }
}
